package top.wardyan.wdev.dbdevassist.config;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import top.wardyan.util.KeyCode;
import top.wardyan.util.ShellUtil;

public class PasswordDialog extends Dialog {

	protected String result = "";
	protected Shell shell;
	private Text text;

	/**
	 * Create the dialog.
	 * @param parent
	 * @param style
	 */
	public PasswordDialog(Shell parent, int style) {
		super(parent, style);
	}

	/**
	 * Open the dialog.
	 * @return the result
	 */
	public String open() {
		createContents();
		shell.open();
		shell.layout();
		Display display = getParent().getDisplay();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		//result = text.getText();
		return result;
	}

	/**
	 * Create contents of the dialog.
	 */
	private void createContents() {
		shell = new Shell(getParent(), SWT.TITLE | SWT.CLOSE);
		shell.setSize(450, 70);
		shell.setText("密码");
		
		Label lblNewLabel = new Label(shell, SWT.NONE);
		lblNewLabel.setAlignment(SWT.RIGHT);
		lblNewLabel.setBounds(0, 9, 80, 20);
		lblNewLabel.setText("请输入密码");
		
		text = new Text(shell, SWT.BORDER);
		text.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent e) {
				if(e.keyCode == KeyCode.ENTER || e.keyCode == KeyCode.SMALL_KEY_BOARD_ENTER) {
					result = text.getText();
					shell.dispose();
				}
			}
		});
		
		text.setTextLimit(100);
		text.setBounds(86, 4, 264, 30);
		
		Button btnOK = new Button(shell, SWT.NONE);
		btnOK.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				result = text.getText();
				shell.dispose();
			}
		});
		btnOK.setBounds(356, 2, 87, 34);
		btnOK.setText("确定");

		ShellUtil.center(shell);
	}

}
